package com.zndroid.logcat.displayer;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.zndroid.logcat.R;
import com.zndroid.logcat.Config;
import com.zndroid.logcat.core.LogInfo;

import java.util.List;

/**
 * Created by lzy on 2021/1/15.
 */
public class LogcatAdapter extends RecyclerView.Adapter<LogcatViewHolder> {
    private List<LogInfo> dataList;
    private Context context;
    private String keyWord = "";

    public LogcatAdapter(List<LogInfo> dataList) {
        this.dataList = dataList;
    }

    public void addItem(LogInfo item) {
        synchronized (LogcatAdapter.class) {
            dataList.add(item);
            notifyDataSetChanged();
        }
    }

    public void clearData() {
        if (null != dataList && dataList.size() > 0) {
            dataList.clear();
        }
        notifyDataSetChanged();
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public LogcatViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_log_item, parent, false);
        return new LogcatViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LogcatViewHolder holder, int position) {
        LogInfo info = dataList.get(position);
        if (null != info) {
            String content = String.format(Config.LOG_SHOW_FORMAT,
                    info.getTime(),
                    info.getProcessId(), info.getThreadId(), context.getPackageName(),
                    info.getPriority(), info.getTag(),
                    info.getContent()
            );
            CharSequence text = content;
            if (!TextUtils.isEmpty(keyWord)) {
                int index = content.indexOf(keyWord);
                if (index == -1) {
                    index = content.toLowerCase().indexOf(keyWord.toLowerCase());
                }
                SpannableString spannable = new SpannableString(content);
                while (index > -1) {
                    int start = index;
                    int end = index + keyWord.length();
                    spannable.setSpan(new BackgroundColorSpan(Color.WHITE), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spannable.setSpan(new ForegroundColorSpan(Color.BLACK), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    index = content.indexOf(keyWord, end);
                    if (index == -1) {
                        index = content.toLowerCase().indexOf(keyWord.toLowerCase(), end);
                    }
                    text = spannable;
                }
            }
            holder.levelTextView.setText(text);
            holder.levelTextView.setTextColor(context.getResources().getColor(Config.getColorRes(info.getPriority())));
        }

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }
}
